package com.jt.www.admin.order_wxb.service;

import com.alibaba.fastjson.JSON;
import com.jt.www.admin.order_wxb.service.impl.WxbService;
import com.jt.www.domain.qo.ConfirmPolicyQo;
import com.jt.www.domain.qo.InvoiceQDXPXQo;
import com.jt.www.model.reps.GenericResponse;
import com.jt.www.model.reps.ResponseData;
import com.jt.www.remote.DexClient;
import com.jt.www.remote.ins.InsClient;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * Created by shaoyn on 2020/09/16.
 */
@Service
public class WxbServiceImpl implements WxbService {

    public static final Logger log = LoggerFactory.getLogger(WxbServiceImpl.class);

    @Autowired
    private InsClient insClient;

    @Autowired
    private DexClient dexClient;

    @Override
    public ResponseData repeatInsurance(ConfirmPolicyQo confirmPolicyQo) throws Exception {
        String confirmPolicyStr = JSON.toJSONString(confirmPolicyQo);
        return insClient.repeatInsurance(confirmPolicyStr);
    }

    @Override
    public ResponseData downInvoiceQDXPX(InvoiceQDXPXQo invoiceQDXPXQo) throws Exception {

        String findCode = "downInvoiceQDXPX_" + System.currentTimeMillis();
        log.info("{} 启东学平险 下载电子发票开始 入参：{}", findCode, invoiceQDXPXQo);
        // todo 下载电子发票 拼装参数
        Document info = DocumentHelper.createDocument();
        Element message = info.addElement("Message");
        message.addElement("PolicyNo").addText(invoiceQDXPXQo.getPolicyNo());
        message.addElement("HolderPhone").addText(invoiceQDXPXQo.getHolderPhone());

        log.info("{} 启东学平险 下载发票报文：{}", findCode, info.asXML());
        String invoiceSeqNo = Long.toString(Math.abs(new Random().nextLong()));
        ResponseEntity<String> responseEntity = null;
        try {
            responseEntity = dexClient.text("edu", "202009181600490", "1.0", invoiceSeqNo, info.asXML());
            log.info("{} 启东学平险 下载发票结束，回参{}", findCode, responseEntity.getStatusCodeValue() + "," + responseEntity.getBody());
            if (responseEntity != null) {
                String root = responseEntity.getBody();
                Document doc = DocumentHelper.parseText(root);
                Element body = doc.getRootElement().element("body");
                String status = body.element("invoiceResults").element("invoiceResults").element("status").getStringValue();
                if ("Effective".equals(status) || "Success".equals(status)) {
                    ResponseData responseData = new ResponseData();
                    responseData.setCode(GenericResponse.CODE_OK);
                    return responseData;
                } else {
                    String msg = body.element("invoiceResults").element("invoiceResults").element("errorMsg").getStringValue();
                    ResponseData responseData = new ResponseData();
                    responseData.setCode(GenericResponse.CODE_NG);
                    responseData.setMsg(msg);
                    return responseData;
                }
            } else {
                ResponseData responseData = new ResponseData();
                responseData.setCode(GenericResponse.CODE_NG);
                responseData.setMsg("调用数据交换中心失败，未返回结果");
                return responseData;
            }
        } catch (Exception e) {
            log.error("{} 启东学平险 电子发票 调用dex服务失败,请重试", findCode);
            e.printStackTrace();

            ResponseData responseData = new ResponseData();
            responseData.setCode(GenericResponse.CODE_NG);
            responseData.setMsg("调用数据交换中心异常");
            return responseData;
        }
    }
}
